<?php
/**
 * Created by PhpStorm.
 * User: o0无忧亦无佈(285753421@qq.com)
 * Date: 16-1-23
 * Time: 下午2:23
 */
class MY_Mongo_Model extends MY_model{
    protected $_table_name = '';
    function __construct(){
        parent::__construct();
        $this->load->library('mongo_db');
    }

    function insert($data){
        return $this->mongo_db->insert($this->_table_name,$data);
    }

    function save($data,$id=''){

        if($id){
            $data['_id']=$id;
        }
        return $this->mongo_db->update($this->_table_name,$data);
    }

    function save_all($data){
        return $this->mongo_db->update_all($this->_table_name,$data);
    }

    function get($data=array(),$limit = 15,$offset = 0){
        if($offset)
            $this->mongo_db->offset($offset);
        $this->mongo_db->limit($limit);
        if(is_array($data) and count($data)>0)
        {
            return $this->mongo_db->get_where($this->_table_name,$data);
        }else{
            return $this->mongo_db->get($this->_table_name);
        }
    }

    public function order_by($fields = array())
    {
        $this->mongo_db->order_by($fields);
    }

    function count(){
        return $this->mongo_db->count($this->_table_name);
    }

    function like($field , $value, $flags = "i", $enable_start_wildcard = TRUE, $enable_end_wildcard = TRUE){
        $this->mongo_db->limit(5);
        return $this->mongo_db->like($field,$value,$flags,$enable_start_wildcard,$enable_end_wildcard);
    }

    function or_where($wheres = array()){
        $this->mongo_db->or_where($wheres);
    }

    function update($collection, $data, $options = array()){
        return $this->mongo_db->update($collection,$data,$options);
    }

    function delete($table_name,$where){
        $this->mongo_db->where($where);
        return $this->mongo_db->delete($table_name);
    }

    function soft_del_mongo($id){
        $options['_id'] = $id;
        $data['is_del'] = 1;
        return $this->mongo_db->update($this->_table_name,$data,$options);
    }

    function delete_all($table_name,$where){
        $this->mongodb->where($where);
        return $this->mongodb->delete_all($table_name);
    }

    function get_by_id($id){
        $data['_id'] = $id;
        $result =  $this->mongo_db->get_where($this->_table_name,$data);
        return $result;
    }

}